import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from '../views/login/Login.vue'
import IndexView from '../views/Pharmacy/Index.vue'
import PharmacyStorage from '../views/Pharmacy/pharmacyStorage/PharmacyStorage.vue'

//线下挂号相关路由
import MainView from '../views/registration/Main.vue'
import SigninView from '../views/registration/Signin.vue'
import RegistView from '../views/registration/Regist.vue'
import ShowRegistView from '../views/registration/ShowRegist.vue'

// 引入管理员相关路由
import AdminView from '../views/admin/AdminView.vue'
import AdminHome from '../views/admin/AdminHome.vue'
import AdminDoctorInfo from '../views/admin/AdminDoctorInfo.vue'
import AdminDeptInfo from '../views/admin/AdminDeptInfo.vue'
import AdminAuthority from '../views/admin/AdminAuthority.vue'

// import { config } from 'vue/types/umd'

import DoctorIndexView from '../views/doctor/Index.vue'


import Error from '../views/error/error.vue'


// 异步
const ShowStorageIn = () =>import('../views/Pharmacy/ShowStorageIn/ShowStorageIn.vue')
const ShowPharmacyIn = () =>import('../views/Pharmacy/showPharmacyIn/ShowPharmacyIn.vue')
const ShowSupplement = () =>import('../views/Pharmacy/showSupplement/showSupplement.vue')
const ShowUnqualifiedMedicine = () =>import('../views/Pharmacy/showUnqualifiedMedicine/showUnqualifiedMedicine.vue')
const ShowPersonal = () => import('../views/Pharmacy/Personal/Personal.vue')
const ShowDispense = () =>import('../views/Pharmacy/showDispense/ShowDispense.vue')

const DoctorDiagnosis = () =>import('../views/doctor/doctorDiagnosis/doctorDiagnosis.vue')
const DoctorSchedule = () =>import('../views/doctor/doctorSchedule/doctorSchedule.vue')

Vue.use(VueRouter)

const routes = [
	{
		  path: '/registration',
		  name: 'MainView',
		  component: MainView,
		  children:[
			  {
				  path:'signin',
				  name:'signin',
				  component: SigninView
			  },
			  {
				  path:'regist',
				  name:'regist',
				  component:RegistView
			  },
			  {
				  path:'showRegist',
				  name:'showRegist',
				  component:ShowRegistView
			  },
		  ]
	},
  {
    path: '',
    name: 'loginView',
    component: LoginView
  },
  {
    path: '/error',
    name: 'error',
    component: Error
  },
  {
    path: '/pharmacy',
    name: 'indexView',
    component: IndexView,
    //路由的元信息，表示该路由以及他的所有子路由
    meta:{
      isLogin:true
    },
    children:[
      {
        path: '',
        name:'pharmacyStorage',
        component: PharmacyStorage
      },
      {
        path: 'showPharmacyIn',
        name:'showPharmacyIn',
        component: ShowPharmacyIn
      },
      {
        path:'ShowStorageIn',
        name:'ShowStorageIn',
        component: ShowStorageIn
      },
      {
        path:'showSupplement',
        name:'showSupplement',
        component: ShowSupplement
      },
      {
        path:'showUnqualifiedMedicine',
        name:'showUnqualifiedMedicine',
        component: ShowUnqualifiedMedicine
      },
      {
        path:'showPersonal',
        name:'showPersonal',
        component: ShowPersonal
      },
	  {
	    path:'showDispense',
	    name:'showDispense',
	    component: ShowDispense
	  },
    ]
  },
  // 配置管理员的路由
  {
    name: 'admin',
    path: '/admin',
    component: AdminView,
    children: [
      {
        path: '',
        component: AdminHome
      },
      {
        name: 'adminHome',
        path: 'home',
        component: AdminHome
      },
      {
        name: 'adminDoctorInfo',
        path: 'doctorInfo',
        component: AdminDoctorInfo
      },
      {
        name: 'adminDeptInfo',
        path: 'deptInfo',
        component: AdminDeptInfo
      },
      {
        name: 'adminAuthority',
        path: 'authority',
        component: AdminAuthority
      },
    ]
  },
  {
    path: '/doctor',
    name: 'doctorIndexView',
    component: DoctorIndexView,
    //路由的元信息，表示该路由以及他的所有子路由
    meta:{
      isLogin:true
    },
    children:[
      {
        path: 'doctorDiagnosis',
        name:'DoctorDiagnosis',
        component: DoctorDiagnosis
      },
      {
        path: 'doctorSchedule',
        name:'DoctorSchedule',
        component: DoctorSchedule
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//需要查看其他页面把该代码注释掉
// 导航守卫
router.beforeEach((to,from,next)=>{
  let token = JSON.parse(localStorage.getItem("user"));
  // console.log(token==null)
  if(to.path != '/'){
    if(token!=null){
      if(to.path.indexOf(token.path) == 1||to.path.indexOf('error')==1){
        next()
      }else{
        next('/')
        localStorage.removeItem("user");
      }
    }else{
      next('/')
      localStorage.removeItem("user");
    }
  }else{
    next()
  }
})

export default router
